/**
 * 
 */
package org.istt.export;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.istt.domain.FileGiaHanCPXD;

/**
 * DON XIN CAP MOI GIAY PHEP XAY DUNG 
 * @author Ndtho
 *
 */
public class BmQT_GPXD_GH extends BaseReport{
	
	public InputStream toMSWord(InputStream docFile, FileGiaHanCPXD f) throws IOException {
	    
	    XWPFDocument docx = new XWPFDocument(docFile);
	    Iterator<XWPFTable> range=docx.getTablesIterator();

	    //Header
        XWPFTable tableHeader=range.next();
        
        //Form Detail
        XWPFTable tableForm=range.next();
        
        //Signature
        XWPFTable tableSignature=range.next();
        
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date today = new Date();
		String date =  dateFormat.format(today);
        
        /*==================== HEADER ====================*/
		
        /*==================== FORM DETAIL ====================*/
        XWPFTableRow row_0 = tableForm.getRow(0);
        setTextFormat(row_0.getCell(0).getParagraphs().get(0).createRun(), f.getHo_ten(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        setTextFormat(row_0.getCell(1).getParagraphs().get(0).createRun(), f.getCmnd_so(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_1 = tableForm.getRow(1);
        setTextFormat(row_1.getCell(0).getParagraphs().get(0).createRun(), f.getCmnd_donvi_cap(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        setTextFormat(row_1.getCell(1).getParagraphs().get(0).createRun(), f.getCmnd_ngay_cap(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_2 = tableForm.getRow(2);
        setTextFormat(row_2.getCell(0).getParagraphs().get(0).createRun(), f.getDia_chi(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        XWPFTableRow row_3 = tableForm.getRow(3);
        setTextFormat(row_3.getCell(0).getParagraphs().get(0).createRun(), f.getSo_nha(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        setTextFormat(row_3.getCell(1).getParagraphs().get(0).createRun(), f.getDuong(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        XWPFTableRow row_4 = tableForm.getRow(4);
        setTextFormat(row_4.getCell(0).getParagraphs().get(0).createRun(), f.getPhuong(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        setTextFormat(row_4.getCell(1).getParagraphs().get(0).createRun(), f.getQuan(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_5 = tableForm.getRow(5);
        setTextFormat(row_5.getCell(0).getParagraphs().get(0).createRun(), f.getDien_thoai(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        XWPFTableRow row_6 = tableForm.getRow(6);
        setTextFormat(row_6.getCell(0).getParagraphs().get(0).createRun(), f.getChu_cong_trinh(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_8 = tableForm.getRow(7);
        setTextFormat(row_8.getCell(0).getParagraphs().get(0).createRun(), f.getCt_so(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        setTextFormat(row_8.getCell(1).getParagraphs().get(0).createRun(), f.getCt_duong(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_9 = tableForm.getRow(8);
        setTextFormat(row_9.getCell(0).getParagraphs().get(0).createRun(), f.getCt_phuong(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        setTextFormat(row_9.getCell(1).getParagraphs().get(0).createRun(), f.getCt_quan(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_10 = tableForm.getRow(9);
        setTextFormat(row_10.getCell(0).getParagraphs().get(0).createRun(), f.getDonvi_cap(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        
        XWPFTableRow row_11 = tableForm.getRow(10);
        setTextFormat(row_11.getCell(0).getParagraphs().get(0).createRun(), f.getGpxd_so(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);
        setTextFormat(row_11.getCell(1).getParagraphs().get(0).createRun(), f.getGpxd_ngay_cap(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        XWPFTableRow row_12 = tableForm.getRow(11);
        setTextFormat(row_12.getCell(0).getParagraphs().get(0).createRun(), f.getGpxd_ly_do_gia_han(), FONT_SIZE_12, false, false, FONT_TIMES_NEW_ROMAN);

        /*==================== SIGNATURE ====================*/
        XWPFTableRow footer_row = tableSignature.getRow(0);
        XWPFRun footer_column = footer_row.getCell(1).getParagraphs().get(0).createRun();
        setTextFormat(footer_column, getText("report.BmQT1201.header.date.first")+ " Ng\u00E0y "+date.substring(0, 2)+" th\u00E1ng "+date.substring(3, 5)+" n\u0103m "+date.substring(6, 10), FONT_SIZE_12, false, true, FONT_TIMES_NEW_ROMAN);

	    //write to word file
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    docx.write(baos);
	    
	    return new ByteArrayInputStream(baos.toByteArray());
	}
}
